import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:psi/routes/app_pages.dart';
import 'package:psi/theme/app_colors.dart';
import 'package:psi/theme/app_theme.dart';

import 'generated/locales.g.dart';


class App extends StatefulWidget {
  const App({Key? key}) : super(key: key);

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 667),
      builder: (context, child) {
        return GetMaterialApp(
          onGenerateTitle: (context) {
            return "PSI";
          },
          theme: appThemeData,
          debugShowCheckedModeBanner: false,
          locale: Get.deviceLocale,
          localizationsDelegates: const [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          fallbackLocale: const Locale('zh', 'CN'),
          translationsKeys: AppTranslation.translations,
          supportedLocales: const [
            // Locale('en', 'US'),
            Locale('zh', 'CN'),
          ],
          navigatorObservers: [FlutterSmartDialog.observer],
          getPages: AppPages.pages,
          initialRoute: PageNames.splash,
          onInit: () {
            // 初始化刷新头部跟底部样式
            EasyRefresh.defaultHeaderBuilder = () => const TaurusHeader(skyColor: primaryColor);
            EasyRefresh.defaultFooterBuilder = () => const BezierFooter(backgroundColor: primaryColor, infiniteOffset: null);
          },
          builder: FlutterSmartDialog.init(
            builder: (context, child) => MediaQuery(
              /// 设置文字大小不随系统设置改变
              data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
              child: Scaffold(
                resizeToAvoidBottomInset: false,
                // Global GestureDetector that will dismiss the keyboard and unFocus node
                body: GestureDetector(
                  onTap: () {
                    FocusScopeNode currentFocus = FocusScope.of(context);
                    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
                      FocusManager.instance.primaryFocus?.unfocus();
                    }
                  },
                  child: child,
                ),
              ),
            ),
            // toastBuilder: (msg) => ToastWidget(msg: msg),
            // loadingBuilder: (msg) => LoadingWidget(msg: msg),
          ),
        );
      },
    );
  }
}
